//
//  FreshFoodBodyView.swift
//  SmartHousekeeper
//
//  Created by 刘森 on 2020/2/21.
//  Copyright © 2020 LS. All rights reserved.
//

import UIKit



class FreshFoodBodyView: UIView {
    
    var firstLevelData: [String]? {
        didSet {
            if self.firstLevelData != nil {
                self.firstLevelTableView.reloadData()
                let indexPath = IndexPath(row: defaultSelectCellIndex, section: 0)
                self.firstLevelTableView.selectRow(at: indexPath, animated: true, scrollPosition: .none)
            }
        }
    }
    
    var secondLevelData: [FreshFoodSecondLevelCellModel]? {
        didSet {
            if self.secondLevelData != nil {
                self.secondLevelTableView.reloadData()
            }
        }
    }
    
    var defaultSelectCellIndex: Int = 0 //默认选中第一个cell
    
    public var firstLevelTableViewCellTouch:((IndexPath) -> ())?
    lazy var firstLevelTableView: UITableView = {
        let value = UITableView()
        value.delegate = self
        value.dataSource = self
        value.tableFooterView = UIView()
        value.showsVerticalScrollIndicator = false
        value.register(FreshFoodFirstLevelCell.self, forCellReuseIdentifier: "FreshFoodFirstLevelCell")
        value.rowHeight = Adapter(66)
        value.tag = 10000 + 1
        value.separatorStyle = .singleLine
        value.separatorInset = UIEdgeInsets(top: 0, left: 0, bottom: 0, right: 0)
        value.backgroundColor = KColorRGBA(248, 245, 242)
        return value
    }()
    
    public var secondLevelTableViewCellTouch:((IndexPath) -> ())?
    lazy var secondLevelTableView: UITableView = {
        let value = UITableView()
        value.delegate = self
        value.dataSource = self
        value.tableFooterView = UIView()
        value.showsVerticalScrollIndicator = false
        value.register(FreshFoodSecondLevelCell.self, forCellReuseIdentifier: "FreshFoodSecondLevelCell")
        value.rowHeight = Adapter(144)
        value.tag = 10000 + 2
        value.separatorStyle = .singleLine
        value.separatorInset = UIEdgeInsets(top: 0, left: 0, bottom: 0, right: 0)
        return value
    }()
    
    lazy var firstLevelPlusLine: UIImageView = {
        let value = UIImageView()
        value.backgroundColor =  KColorRGBA(202, 202, 202)
        return value
    }()
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        self.addSubviews([firstLevelTableView, firstLevelPlusLine, secondLevelTableView])
        firstLevelTableView.reloadData()
        
        
    }
    
    override func layoutSubviews() {
        firstLevelTableView.snp.makeConstraints { (make) in
            make.top.left.bottom.equalToSuperview()
            make.width.equalTo(Adapter(88))
        }
        firstLevelPlusLine.snp.makeConstraints { (make) in
            make.top.bottom.height.equalToSuperview()
            make.left.equalTo(firstLevelTableView.snp.right)
            make.width.equalTo(1)
        }
        secondLevelTableView.snp.makeConstraints { (make) in
            make.top.right.bottom.equalToSuperview()
            make.left.equalTo(firstLevelPlusLine.snp.right)
        }
    }
    
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
}

extension FreshFoodBodyView: UITableViewDelegate, UITableViewDataSource{
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        switch tableView.tag {
        case 10001:
            return self.firstLevelData?.count ?? 0
        default:
            return self.secondLevelData?.count ?? 0
        }
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        switch tableView.tag {
        case 10001:
            let cell: FreshFoodFirstLevelCell =  FreshFoodFirstLevelCell.baseCell(tableView, cellID: "FreshFoodFirstLevelCell") as! FreshFoodFirstLevelCell
            cell.name = firstLevelData?[indexPath.row, true] ?? ""
            return cell
        default:
            let cell: FreshFoodSecondLevelCell = FreshFoodSecondLevelCell.baseCell(tableView, cellID: "FreshFoodSecondLevelCell") as! FreshFoodSecondLevelCell
            if let model = secondLevelData?[indexPath.row, true] {
                cell.model = model
            }
            cell.restartCount()
            return cell
        }
        
    }
    
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        switch tableView.tag {
        case 10001:
            firstLevelTableViewCellTouch?(indexPath)
        default:
            secondLevelTableViewCellTouch?(indexPath)
        }
    }
    
}
